﻿#region Using Directives

using System;
using System.Text.RegularExpressions;

#endregion Using Directives

namespace SharePointStu.CAMLHelper.General
{
	/// <summary>
	/// Helper class that provides utility methods.
	/// </summary>
	internal class Helper
	{
		/// <summary>
		/// Regular expression for validating a string as a Guid.
		/// </summary>
		private static readonly Regex isGuid = new Regex(@"^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$", RegexOptions.Compiled);

		/// <summary>
		/// Check if passed string is a valid Guid.
		/// </summary>
		/// <param name="candidate">String to test as a valid Guid.</param>
		/// <param name="output">Guid variable.</param>
		/// <returns>bool: If the candidate string is a valid Guid</returns>
		internal static bool IsGuid(string candidate, out Guid output)
		{
			bool isValid = false;
			output = Guid.Empty;

			if (!string.IsNullOrEmpty(candidate))
			{
				if (isGuid.IsMatch(candidate))
				{
					output = new Guid(candidate);
					isValid = true;
				}
			}
			return isValid;
		}
	}
}
